﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace AugustVisual
{
    public class FrameworkElementHelper
    {

        public static bool IsInDesignMode => (bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue;

        public static T FindParent<T>(FrameworkElement child, int num, string name)
        {
            int findnum = 0;
            FrameworkElement parent = child;
            while (true)
            {
                parent = (FrameworkElement)VisualTreeHelper.GetParent(parent);
                if (parent == null)
                {
                    break;
                }
                if (parent is T)
                {
                    if (name == null)
                    {
                        findnum++;
                        if (num == findnum)
                        {
                            break;
                        }
                    }
                    else if (parent.Name == name)
                    {
                        findnum++;
                        if (num == findnum)
                        {
                            break;
                        }
                    }
                    else
                    {

                    }
                }
            }
            return (T)(object)parent;
        }



        public static FrameworkElement Findchild(FrameworkElement element, string name)
        {
            if (VisualTreeHelper.GetChildrenCount(element) > 0)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
                {
                    DependencyObject dependencyObject = VisualTreeHelper.GetChild(element, i);
                    FrameworkElement frameworkElement = (FrameworkElement)dependencyObject;
                    if (frameworkElement.Name == name)
                    {
                        return frameworkElement;
                    }
                    FrameworkElement ret = Findchild((FrameworkElement)dependencyObject, name);
                    if (ret != null)
                    {
                        return ret;
                    }
                }
            }
            return null;
        }

        public static FrameworkElement[] Findchilds(FrameworkElement element, Type childType)
        {
            List<FrameworkElement> frameworkElements = new List<FrameworkElement>();
            if (VisualTreeHelper.GetChildrenCount(element) > 0)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
                {
                    DependencyObject dependencyObject = VisualTreeHelper.GetChild(element, i);
                    FrameworkElement frameworkElement = (FrameworkElement)dependencyObject;
                    if (frameworkElement.GetType() == childType)
                    {
                        frameworkElements.Add(frameworkElement);
                    }
                    FrameworkElement[] ret = Findchilds((FrameworkElement)dependencyObject, childType);
                    frameworkElements = ret.Concat(frameworkElements).ToList();
                }
            }
            return frameworkElements.ToArray();
        }

    }
}

